import mindspore.numpy
import numpy as np

# def step(x):
#     y = x > 0
#     return y.astype(np.int32)

from mindspore import Tensor
from mindspore import dtype
import matplotlib.pylab as plt


# x=Tensor(np.arange(-8.0,8.,.1),dtype.float32)
# y=step(x)
# plt.plot(x,y)
# plt.ylim(-0.1,1.1)
# plt.show()
def step(x):
    y = x > 0
    return np.array(x > 0, dtype=np.int_)


def AND(x1, x2):
    x = Tensor(np.array([x1, x2]), dtype.float32)
    w = Tensor(np.array([0.3, .3]), dtype.float32)
    b = -.5
    y = mindspore.numpy.sum(w * x) + b
    return step(y)


# print(AND(0,0))
# print(AND(0,1))
# print(AND(1,0))
# print(AND(1,1))

# x1, x2 = 1, 1


def h(x):
    return np.array(x > 0, dtype=np.int_)


def NAND(x1, x2):
    x = Tensor(np.array([x1, x2]), dtype.float32)
    w1 = Tensor(np.array([-.5, -.5]), dtype.float32)
    b1 = -.5
    y1 = mindspore.numpy.sum(w1 * x) + b1
    return h(y1)


def OR(x1, x2):
    x = Tensor(np.array([x1, x2]), dtype.float32)
    w = Tensor(np.array([.5, .5]), dtype.float32)
    b = -.4
    y2 = mindspore.numpy.sum(w * x) + b
    return h(y2)


#
#
# print(NAND(x1, x2))
# print(OR(x1, x2))
import mindspore

x1, x2 = 0, 1


def h(x):
    return np.array(x > 0, dtype=np.int_)


exp = mindspore.ops.Exp()


def sigmoid(x):
    return 1 / (1 + exp(-x))


# x=Tensor(np.arange(-8,8,0.1),dtype.float32)
# y=sigmoid(x)
# plt.plot(x,y)
# plt.ylim(-.1,1.1)
# plt.show()

# maxium=mindspore.ops.Maximum()
# x=Tensor(np.arange(-1.,1.1,.1),dtype.float32)
# y=maxium(0,x)
# plt.plot(x,y)
# plt.ylim(-.1,1.1)
# plt.show()
from mindspore import Tensor, nn


def init_network():
    network = {}
    network['w1'] = Tensor(np.random.normal(0, 1, [3, 2]), dtype.float32)
    network['b1'] = Tensor(np.random.normal(0, 1, [3]), dtype.float32)
    network['w2'] = Tensor(np.random.normal(0, 1, [3, 3]), dtype.float32)
    network['b2'] = Tensor(np.random.normal(0, 1, [3]), dtype.float32)
    network['w3'] = Tensor(np.random.normal(0, 1, [2, 3]), dtype.float32)
    network['b3'] = Tensor(np.random.normal(0, 1), [2], dtype.float32)
    return network

class Net(nn.Cell):
    def __int__(self):
        super(Net, self).__init__()
        network = init_network()
        w1, w2, w3 = network['w1'], network['w2'], network['w3']
        b1, b2, b3 = network['b1'], network['b2'], network['b3']
        self.fc1 = nn.Dense(2, 3, w1, b1, True, 'sigmoid')
        self.fc2 = nn.Dense(3, 3, w2, b2, True, 'sigmoid')
        self.fc3 = nn.Dense(3, 2, w3, b3, True)

    def construct(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x


# input=Tensor(np.array([[1.0,2.0]]),dtype.float32)
# model=Net()
# output=model(input)
# print(output)

input = Tensor(np.array([[1.0, 2.0]]), dtype.float32)
model = Net()
output = model(input)
print(output)
